package com.gavindon.android.view.record

import android.os.Bundle
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.leanback.tab.LeanbackTabLayout
import androidx.leanback.tab.LeanbackViewPager
import androidx.leanback.widget.VerticalGridView
import com.gavindon.android.R
import com.gavindon.android.adapters.LeftRecordEnum
import com.gavindon.android.adapters.RecordLeftAdapter
import com.gavindon.android.base.BaseActivity
import com.gavindon.android.base.INTENT_FRAGMENT_RECORD
import com.gavindon.android.entity.RecordTab
import com.gavindon.android.viewmodel.CommonViewModel

/**
 * description:个人中心 我的收藏 我的学习
 * Created by liNan on  2022-2-11 10:50
 */
class RecordActivity : BaseActivity() {
    override val layoutId = R.layout.activity_record


    private lateinit var mViewPager: LeanbackViewPager
    lateinit var mTabLayout: LeanbackTabLayout
    private lateinit var leftRv: VerticalGridView
    private lateinit var mViewModel: CommonViewModel

    //判断进入页面显示学习还是收藏
    private val index: Int by lazy {
        intent.getIntExtra("index", 0)
    }
    private val leftAdapter: RecordLeftAdapter =
        RecordLeftAdapter(R.layout.adapter_education_left, mutableListOf("我的收藏", "我的学习"))

    private var pageAdapter: RecordViewPagerAdapter? = null

    override fun onInit(savedInstanceState: Bundle?) {
        mViewPager = findViewById(R.id.record_lean_view_pager)
        mTabLayout = findViewById(R.id.record_leantab)
        pageAdapter = RecordViewPagerAdapter(supportFragmentManager, null, index)
        initLeft()
        initTab(index)
    }

    private fun initLeft() {
        leftRv = findViewById(R.id.rvLeftRecord)
        leftRv.setNumColumns(1)
        leftRv.adapter = leftAdapter
        leftAdapter.setFocusChangeListener {
            initTab(it.ordinal)
        }
        //选中左侧
        leftRv.layoutManager?.scrollToPosition(index)
//        leftAdapter.setDpadListener {
//            if (it.name == DirectionEnum.RIGHT.name) {
//                mViewPager.requestFocus()
//            }
//        }
    }

    private fun initTab(leftIndex: Int = 0) {
        if (mTabLayout.tabCount > 0) {
            mTabLayout.removeAllTabs()
            pageAdapter?.setNewTab(null, leftIndex)
            mViewPager.adapter = pageAdapter
        }
        val tabs =
            if (leftIndex == 0) listOf(RecordTab("课程", 0), RecordTab()) else listOf(RecordTab("记录"))
        pageAdapter?.setNewTab(tabs, leftIndex)
        mViewPager.adapter = pageAdapter
        mTabLayout.setupWithViewPager(mViewPager)
        tabs.forEachIndexed { index, s ->
            val tab = mTabLayout.getTabAt(index)
            tab?.let {
                val tabView = layoutInflater.inflate(R.layout.custom__record_tab, null, false)
                tabView.findViewById<TextView>(R.id.custom_tab_tv).text = s.tabName
                it.customView = tabView
            }
        }
    }

}

//两个状态 左侧 tabLayout
class RecordViewPagerAdapter(fm: FragmentManager, dataLst: List<RecordTab>?, leftIndex: Int) :
    FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

    private var tabData: List<RecordTab>? = dataLst

    private var leftIdx = leftIndex

    override fun getCount() = tabData?.size ?: 0

    fun setNewTab(tabs: List<RecordTab>?, leftIndex: Int) {
        this.tabData = tabs
        this.leftIdx = leftIndex
        notifyDataSetChanged()
    }

    override fun getItem(position: Int): Fragment {
        val fragment = RecordFragment()
        fragment.arguments = bundleOf(
            Pair(INTENT_FRAGMENT_RECORD, tabData?.get(position)),
            Pair("leftIndex", leftIdx)
        )
        return fragment
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return tabData?.get(position)?.tabName
    }

}
